/**
 * @author Kristian Mandrup
 * @date June 10, 2009
 * @version 0.1
 *
 * jsTree.generator :: eventHandlers
 *
 * Default eventhandler to handle events received from eventController
 * 
 * Public event namespaces
 * -----------------------
 * - bc 	: onBegin... and onComplete... events
 * - info	: Info events
 * - warning: Warning events
 * - error	: Error events (should abort generator)
 *
 */
m3gen.eventHandler = {
	// begin/complete (lower importance level than info!)
	bc: {
        onBeginSelectId: function(event){
            var log = m3gen.log;
            log('SelectId (started)');
        },
        onBeginSelectTitle: function(event){
            var log = m3gen.log;
            log('SelectTitle (started)');
        },
        onBeginSelectId: function(event){
            var log = m3gen.log;
            log('SelectId (started)');
        },
        onBeginSelectState: function(event){
            var log = m3gen.log;
            log('SelectState (started)');
        },
        
        onBeginGenerateTreeInstanceModel: function(event){
            var log = m3gen.log;
            var name = event.instanceName;
            if (name && name != '') 
                log('GenerateTreeInstanceModel for instance [' + name + '] (started)');
            else 
                log('GenerateTreeInstanceModel for instance (started)');
        },
        onCompleteGenerateTreeInstanceModel: function(event){
            var log = m3gen.log;
            var name = event.instanceName;
            if (name && name != '') 
                log('GenerateTreeInstanceModel for instance [' + name + '] (completed OK)');
            else 
                log('GenerateTreeInstanceModel for instance (completed OK)');
        },
        onBeginParseInstance: function(event){
            var log = m3gen.log;
            var name = event.instanceName;
            var rootObj = event.rootObj;
            if (name && name != '') 
                log('ParseInstance for instance [' + name + ']  with ' + rootObj.length + ' items (started)');
            else 
                log('ParseInstance for instance with ' + rootObj.length + ' items (started)');
        },
        onCompleteParseInstance: function(event){
            var log = m3gen.log;
            var name = event.instanceName;
            var jsTreeModel = event.jsTreeModel;
            log('ParseInstance for instance [' + name + '] generated jsTree model with ' + jsTreeModel.length + ' nodes (completed OK)');
        },
        onBeginParseInstanceItem: function(event){
            var log = m3gen.log;
            log('ParseInstanceItem (started)');
        },
        onCompleteParseInstanceItem: function(event){
            var log = m3gen.log;
            var jsTreeNode = event.jsTreeNode;
            if (jsTreeNode) {
                if (jsTreeNode.attributes) {
                    var id = jsTreeNode.attributes.id;
                    var children = jsTreeNode.children;
                    if (children && children.length > 0) 
                        log('ParseInstanceItem [' + id + '] with ' + jsTreeNode.children.length + ' children (completed OK)');
                    else 
                        log('ParseInstanceItem [' + id + '] (completed OK)');
                }
                else 
                    log('ParseInstanceItem - no attributes (completed ERROR)');
            }
            else 
                log('ParseInstanceItem (completed ERROR)');
        },
        onBeginParseWithGeneratorRuleSet: function(event){
            var log = m3gen.log;
            log('Parsing with generatorRuleSet [' + event.context.ruleSetConfiguration.name + '] (started)');
        },
        
        onBeginResolveChildren: function(event){
            var log = m3gen.log;
            var id = event.jsTreeNode.attributes.id;
            var selectors = event.selectors;
            if (selectors && selectors.length > 0) 
                log('ResolveChildren for [' + id + '] with ' + selectors.length + ' selectors (started)');
            else 
                log('ResolveChildren for [' + id + '] (started)');
        },
        onCompleteResolveChildren: function(event){
            var log = m3gen.log;
            var children = event.children;
            if (children && children.length > 0) 
                log('ResolveChildren for [' + id + '] generated ' + children.length + ' children (completed OK)');
            else 
                log('ResolveChildren for [' + id + '] (completed OK)');
        },
        onBeginCreateBranchNode: function(event){
            var log = m3gen.log;
            var id = event.jsTreeNode.attributes.id;
            var branchNode = event.branchNode;
            var branchNode_id = branchNode.id;
            var branchNode_title = branchNode.title;
            if (branchNode && branchNode.selectors) 
                log('CreateBranchNode for [' + id + '] titled "' + branchNode.title + '" with ' + branchNode.selectors.length + ' selectors (started)');
            else 
                log('CreateBranchNode for [' + id + '] titled "' + branchNode.title + '" (started)');
        },
        onCompleteCreateBranchNode: function(event){
            var log = m3gen.log;
            var jsTreeNode = event.jsTreeNode;
            var jsTreeBranchNode = event.jsTreeBranchNode;			
            var id = jsTreeNode.attributes.id;
            var branchNode_title = event.branchNode_title;
            var branchNode_id = jsTreeBranchNode.attributes.id;
            log('CreateBranchNode for [' + id + '] titled "' + branchNode_title + '" generated branch node [' + branchNode_id + '] (completed OK)');
        },		
        onBeginResolveBranch: function(event){
            var log = m3gen.log;
            var id = event.jsTreeNode.attributes.id;
            var branch = event.branch;
            if (branch && branch.selectors) 
                log('ResolveBranch for [' + id + '] with ' + branch.selectors.length + ' selectors (started)');
            else 
                log('ResolveBranch for [' + id + '] (started)');
        },
        onCompleteResolveBranch: function(event){
            var log = m3gen.log;
            var id = event.jsTreeNode.attributes.id;
            var children = event.children;
            if (children && children.length > 0) 
                log('ResolveBranch for [' + id + '] generated ' + children.length + ' children (completed OK)');
            else 
                log('ResolveBranch for [' + id + '] with no children (completed OK)');
            
        },
        onBeginResolveBranches: function(event){
            var log = m3gen.log;
            var id = event.jsTreeNode.attributes.id;
            var branches = event.branches;
            if (branches && branches.length > 0) 
                log('ResolveBranches for [' + id + '] with ' + branches.length + ' branches (started)');
            else 
                log('ResolveBranches for [' + id + '] with no branches (started)');
        },
        onCompleteResolveBranches: function(event){
            var log = m3gen.log;
            var id = event.jsTreeNode.attributes.id;
            var children = event.children;
            if (children && children.length > 0) 
                log('ResolveBranches for [' + id + '] generated ' + children.length + ' children (completed OK)');
            else 
                log('ResolveBranches for [' + id + '] with no children (completed OK)');
        },
        onBeginSetNode: function(event){
            var log = m3gen.log;
            var nodeType = event.nodeType;
            log('SetNode for a [' + nodeType + '] object (started)');
        },
        onCompleteSetNode: function(event){
            var log = m3gen.log;
            var id = event.jsTreeNode.attributes.id;
            log('SetNode for [' + id + '] (completed OK)');
        },
        onBeginSelectIcon: function(event){
            var log = m3gen.log;
            log('SelectIcon (started)');
        },		
        onBeginSelectId: function(event){
            var log = m3gen.log;
            log('SelectId (started)');
        },
        onBeginSelectState: function(event){
            var log = m3gen.log;
            log('SelectState (started)');
        },		
        onBeginSelectTitle: function(event){
            var log = m3gen.log;
            log('SelectTitle (started)');
        },		
        onBeginSelectDataAttributes: function(event){
            var log = m3gen.log;
            log('SelectDataAttributes (started)');
        },		
        onBeginBuildData: function(event){
            var log = m3gen.log;
            if (event.jsTreeNode.attributes) {
                var id = event.jsTreeNode.attributes.id;
                log('BuildData for [' + id + '] (started)');
            }
            else 
                log('BuildData (started)');
        },
        onBeginBuildAttributes: function(event){
            var log = m3gen.log;
            log('BuildAttributes  (started)');
        },
        onBeginBuildState: function(event){
            var log = m3gen.log;
            if (event.jsTreeNode.attributes) {
                var id = event.jsTreeNode.attributes.id;
                log('BuildState for [' + id + '] (started)');
            }
            else 
                log('BuildState (started)');
        },
        
        onBeginResolveSimpleObject: function(event){
            var log = m3gen.log;
            log('ResolveSimpleObject (started)');
            console.log(event.parseObj);
        },
        onCompleteResolveSimpleObject: function(event){
            var log = m3gen.log;
            log('ResolveSimpleObject (completed OK)');
            console.log(event.parseObj);
        },		
        onBeginResolveObject: function(event){
            var log = m3gen.log;
            log('ResolveObject (started)');
            console.log(event.parseObj);
        },
        onCompleteResolveObject: function(event){
            var log = m3gen.log;
            log('ResolveObject (completed OK)');
            console.log(event.parseObj);
        },
        
        onBeginResolveArrayObject: function(event){
            var log = m3gen.log;
            log('ResolveArrayObject with ' + event.arrayObj.length + ' children (started)');
            console.log(event.parseObj);
        },
        onCompleteResolveArrayObject: function(event){
            var log = m3gen.log;
            log('ResolveArrayObject (completed OK)');
            console.log(event.parseObj);
        },	
		onBeginSelector: function(event){
            var log = m3gen.log;
            log('Selector (started)');
        },	
		onCompleteSelector: function(event){
            var log = m3gen.log;
            log('Selector (completed OK)');
        }				
	},
    info: {

		onNotValidJsTreeBranchNode: function(event){
            var log = m3gen.log;
            if (branch && branch.selectors) 
                log('Not a valid jsTree branch node');
        },
		

        
        onFoundJsonReference: function(event){
            var log = m3gen.log;
            log('Found a JSON reference [' + event.jsonRef + ']');
            console.log(event.parseObj);
        },

        onFoundUnresolvedJsonReference: function(event){
            var log = m3gen.log;
            log('Found unresolved JSON reference');
            console.log(event.parseObj);
        },
        onNoSelectorsFound: function(event){
            var log = m3gen.log;
            log('No selectors found for [' + event.branchNode + ']');
            console.log(event.parseObj);
        },
		onFoundNoBranchNode: function(event){
            var log = m3gen.log;
            log('No branchNode found');
        },

		onUseSelectorPath: function(event){
            var log = m3gen.log;
            log('Using Selector path "' + event.selector + '" (started)');
        },
		onFoundObject: function(event){
            var log = m3gen.log;
            log('Found object');
        },

		onCallSelectorFunction: function(event){
            var log = m3gen.log;
            log('Calling Selector function');
        },
		onNotFoundObject : function(event){
            var log = m3gen.log;
            log('No object found');
        },	
		onBranchNodeAddedToChildren: function(event){
            var log = m3gen.log;
            log('Branch node added to children');
        },
		onEmptyBranchNodeNotAdded: function(event){
            var log = m3gen.log;
			var id = event.jsTreeNode.attributes.id;
            log('Empty Branch node not added to children of [' + id + ']');
        },
		onEmptyArrayObjectFound: function(event){
            var log = m3gen.log;
			var id = event.jsTreeNode.attributes.id;
            log('Empty array object found which can not be resolved ');
        },
		onTriedResolveJsonBefore: function(event){
            var log = m3gen.log;
            log('Tried to Resolve JSON structure before without success, using $ref as is');
        },
		onResolveJsonReference: function(event){
            var log = m3gen.log;
            log('Resolving JSON reference');
        },
		onRecursiveObjectEncounter: function(event){
            var log = m3gen.log;
			var id = event.jsTreeNode.attributes.id;
			var count = event.ids[id] || 1;
            log('RecursiveObjectEncounter [' + id + '] repeated ' + count + ' times');
        }
    },
    warning: {
        // WARNING HANDLERS
        // nodeType is null if no RuleSet mapping is found 
        onNoRuleSetForObjTypeWarning: function(event){
            var log = m3gen.log;
            var objType = event.objType;
            if (!objType) {
                log('WARNING: No RuleSet for objType ' + objType + ' found (WARNING)');
            }
            else {
                log('WARNING: RuleSet for objType ' + objType + ' not enabled (WARNING)');
            }
        },
        onMalformedBranchWarning: function(event){
            var log = m3gen.log;
            log('WARNING: Malformed branch ' + branch + ' (WARNING)');
        },
        onResolveObjectWarning: function(event){
            var log = m3gen.log;
            log('WARNING: Could not resolve object (WARNING)');
            console.log(event.parseObj);
        },
        onNoJsTreeNodeWarning: function(event){
            var log = m3gen.log;
            log('WARNING: No jsTree node generated for object (WARNING)');
           console.log(event.parseObj);
        },
        onInvalidJsTreeNodeWarning: function(event){
            var log = m3gen.log;
            log('WARNING: Invalid jsTree node generated for object (WARNING)');
            console.log(event.parseObj);
        }
    },
    error: {
        // ERROR HANDLERS
        onNoGeneratorRuleSetError: function(event){
            var log = m3gen.log;
            log('ERROR: No Generator RuleSet found (ERROR)');
        },
        onMalformedGeneratorRuleSetError: function(event){
            var log = m3gen.log;
            log('ERROR: Malformed Generator RuleSet ' + event.generatorRuleSet.name + ' (ERROR)');
        },
        onNoGeneratorModelError: function(event){
            var log = m3gen.log;
            log('ERROR: No Generator Model found (ERROR)');
        },
		OnNoInstanceModelError: function(event){
            log('ERROR: No Instance Model found (ERROR)');
        }
		
    }
}
